<?php
/**
 * author      :chenwenwen
 * createTime  :2020-04-06 13:03
 * description :
 */

namespace app\admin\controller;

use think\facade\View;

class Login extends Base {

    public function initialize() {
        if ($this->isLogin()) return $this->redirect(url('index/index'));
    }


    public function index() {
        return View::fetch();
    }


    public function check() {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '请求方式错误');
        }
        //参数验证 1。原始方式 2。tp6验证机制
        $username = $this->request->param('username', '', 'trim');
        $password = $this->request->param('password', '', 'trim');
        $captcha = $this->request->param('captcha', '', 'trim');

        $data = [
            'username' => $username,
            'password' => $password,
            'captcha' => $captcha
        ];
        $validate = new \app\admin\validate\AdminUser();
        if (!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //business
        try{
            $result = \app\admin\business\AdminUser::login($data);
        }catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if(empty($result)) {
            return show(config('status.error'),'登录失败');
        } else {
            return show(config('status.success'),'登录成功');
        }


        return $result;
    }

    public function test() {
        dump(config("admin.session_admin"));
        halt(session(config('admin.session_admin')));
    }


}